#include "bsp_Serial.h"
#include "usart.h"

void bsp_Serial::init()
{
    rxTemp = 0;
    memset(rxBuffer, 0, SERIAL_BUFFER_LEN);
    clear();
    BSP_UART_Receive_IT(huart, &rxTemp, 1); // 开启接收中断
}

bsp_Serial::Serial_ret bsp_Serial::send(const char *buffer, int size)
{
    if (BSP_UART_Transmit(huart, buffer, size, SERIAL_TIMEOUT) != BSP_OK)
    {
        SysError("SERIAL_ERROR\r\n");
        return SERIAL_ERROR;
    }
    else
        return SERIAL_OK;
}

bsp_Serial::Serial_ret bsp_Serial::receive(char *buffer, int size, int timeout)
{
    if (popArray((QUEUE_DATA_T *)buffer, size) != QUEUE_OK)
    {
        SysError("SERIAL_ERROR\r\n");
        return SERIAL_ERROR;
    }
    else
        return SERIAL_OK;
}

void bsp_Serial::rxCallback()
{
    push(rxTemp);
}

#if DEMO_SERIAL
bsp_Serial demoSerial(&huart2);
#endif
